package com.tools_s7.activity;

import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

import com.tools.s7.recycler.utils.StaggeredHelper;
import com.tools_s7.R;
import com.tools_s7.adapter.StaggeredAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 瀑布流
 */
public class StaggeredActivity extends AppCompatActivity {

    private RecyclerView recycler;
    private List<String> datas;
    private StaggeredHelper mHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_staggered);

        recycler = findViewById(R.id.staggered_recycler);
        datas = new ArrayList<>();
        mHelper = new StaggeredHelper.Builder<String>()
                .setRecycler(recycler, new StaggeredGridLayoutManager(2,
                        StaggeredGridLayoutManager.VERTICAL))
                .setAadditional(true)
                .setAdapter(new StaggeredAdapter(), true)
                .setLoadData(new StaggeredHelper.LoadDataListener() {
                    @Override
                    public void onLoadData(int page) {
                        getData(page);
                    }
                }).build();

    }

    @Override
    protected void onResume() {
        super.onResume();
        mHelper.loadFirstPage();
    }

    private int s = 0;
    public void getData(int page) {
        if (page == 1) {
            s = 0;
        }

        int e = page * 10;

        if (page == 3) {
            e = e - 3;
        }

        datas.clear();

        for (int i=s; i<e; i++) {
            datas.add("你好" + i);
        }
        s = e;
        CountDown(3);
    }

    public void CountDown(int s) {
        new CountDownTimer(s * 1000, 1000) {
            @Override
            public void onTick(long l) {

            }

            @Override
            public void onFinish() {
                mHelper.notifyAdapter(datas);
//                adapter.addData(datas);
            }
        }.start();
    }
}
